package com.example.myapplication.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.example.myapplication.R;
import com.example.myapplication.bean.MoneyData;
import com.example.myapplication.util.MyDateUtil;

import java.util.Date;

/**
 * 主界面
 *
 * @author xiongwei
 * @date 2020-10-8
 */
public class MainActivity extends AppCompatActivity {

    private Button happyParkButton;
    private Button shYuButton;
    private Button shundeButton;
    private Button monthButton;
    private CalendarView calendarView;
    private EditText startTimeText;
    private EditText endTimeText;
    private EditText moneyText;
    private EditText dayText;

    private String selectDay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        startTimeText = findViewById(R.id.start_time_editText);
        endTimeText = findViewById(R.id.end_time_editText);
        moneyText = findViewById(R.id.money_editText);
        dayText = findViewById(R.id.day_editText);

        happyParkButton = findViewById(R.id.happy_pack_button);
        shYuButton = findViewById(R.id.sh_yu_button);
        shundeButton = findViewById(R.id.shun_de_button);
        monthButton = findViewById(R.id.month_activity_button);

        happyParkButton.setOnClickListener(moneyClickListener);
        shYuButton.setOnClickListener(timeClickListener);
        shundeButton.setOnClickListener(shundeClickListener);
        monthButton.setOnClickListener(monthClickListener);

        calendarView = findViewById(R.id.mian_calendarView);
        calendarView.setOnDateChangeListener(dataListener);

        dayText.setText(DateUtil.format(new Date(System.currentTimeMillis()), DatePattern.NORM_DATE_PATTERN));
        startTimeText.setText(DateUtil.format(new Date(System.currentTimeMillis()), DatePattern.NORM_DATETIME_MINUTE_PATTERN));
        endTimeText.setText(DateUtil.format(new Date(System.currentTimeMillis()), DatePattern.NORM_DATETIME_MINUTE_PATTERN));
    }

    private MoneyData getData() {
        MoneyData moneyData = new MoneyData();
        moneyData.setDayTime(dayText.getText().toString());
        moneyData.setStartTime(startTimeText.getText().toString());
        moneyData.setEndTime(endTimeText.getText().toString());
        moneyData.setMoney(moneyText.getText().toString());
        return moneyData;
    }

    /**
     * moneyTextView跳转happyActivity。
     */
    OnClickListener moneyClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(MainActivity.this, HappyActivity.class);
        }
    };

    /**
     * timeTextView跳转shYuActivity。
     */
    OnClickListener timeClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(MainActivity.this, ShYuActivity.class);
        }
    };

    /**
     * timeTextView跳转shYuActivity。
     */
    OnClickListener shundeClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(MainActivity.this, ShunDeActivity.class);
        }
    };

    /**
     * timeTextView跳转shYuActivity。
     */
    OnClickListener monthClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(MainActivity.this, MonthActivity.class);
        }
    };

    private void startActivity(Context packageContext, Class<?> cls) {
        Intent intent = new Intent(packageContext, cls);
        MoneyData moneyData = getData();
        intent.putExtra("moneyData", moneyData);
        startActivity(intent);
    }

    CalendarView.OnDateChangeListener dataListener = new CalendarView.OnDateChangeListener() {
        @Override
        public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
            selectDay = year + "-" + String.format("%02d", month + 1) + "-" + String.format("%02d", dayOfMonth);
            String time = selectDay + " " + "18:15";
            startTimeText.setText(time);
            endTimeText.setText(time);
            dayText.setText(selectDay);
        }
    };
}
